﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Text.RegularExpressions;
using System.Web;

namespace Isha.Extensions
{
    public static class ContentHelper
    {
        public static string Serializer(Type type, object obj)
        {
            if (obj == null)
                throw new InvalidOperationException("Object can't be null.");

            XmlSerializer ser = new XmlSerializer(type.GetType());
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            ser.Serialize(writer, obj);
            writer.Close();
            return sb.ToString();
        }

        public static dynamic Deserializer(Type type, string xml)
        {
            if (string.IsNullOrEmpty(xml))
                return Activator.CreateInstance(type, false);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer ser = new XmlSerializer(type);
            return ser.Deserialize(reader);
        }       

        //public string UrlDecode(string url)
        //{
        //    return HttpContext.Current.Server.UrlDecode(url);
        //}

        //public string UrlEncode(string url)
        //{
        //    return HttpContext.Current.Server.UrlEncode(url);
        //}

        //public string HtmlEncode(string html)
        //{
        //    return HttpContext.Current.Server.HtmlEncode(html);
        //}

        //public string HtmlDecode(string html)
        //{
        //    return HttpContext.Current.Server.HtmlDecode(html);
        //}
    }
}
